<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:events:add']">新建活动</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:events:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动名称" align="center" prop="name" />
      <el-table-column label="活动类型" align="center" prop="type" />
      <el-table-column label="主办单位" align="center" prop="member_unit_name" />
      <el-table-column label="级别" align="center" prop="level" />
      <el-table-column label="活动开始时间" align="center" prop="events_start_time" width="180">
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="events_end_time" width="180">
      </el-table-column>
      <el-table-column label="活动地点" align="center" prop="events_address" />
      <el-table-column label="报名时间" align="center" prop="sign_time" width="180">
      </el-table-column>
      <el-table-column label="报名费用" align="center" prop="sign_price" />
      <el-table-column label="报名单位数" align="center" prop="sign_unit" />
      <el-table-column label="参数人数" align="center" prop="join_num" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ activity_status[scope.row.activity_status].value }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:events:edit']">提前结束</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level" placeholder="请输入级别" />
        </el-form-item>
        <el-form-item label="主办单位" prop="level">
          <el-select v-model="form.unit_id">
            <el-option v-for="dicts in dict" :key="dicts.id" :label="dicts.member_unit_name" :value="dicts.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="活动开始时间" prop="events_start_time">
          <el-date-picker clearable v-model="form.events_start_time" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择活动时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="events_end_time">
          <el-date-picker clearable v-model="form.events_end_time" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择活动时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动地点" prop="events_address">
          <el-input v-model="form.events_address" placeholder="请输入活动地点" />
        </el-form-item>
        <el-form-item label="报名时间" prop="sign_time">
          <el-date-picker clearable v-model="form.sign_time" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择报名时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名费用" prop="sign_price">
          <el-input v-model="form.sign_price" placeholder="请输入报名费用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvents, getEvents, delEvents, addEvents, updateEvents } from "@/api/events/events";
import { listUnit } from "@/api/unit/unit";
export default {
  name: "Events",
  data() {
    return {
      dict: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      eventsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        unit_id: null,
        level: null,
        events_time: null,
        events_address: null,
        sign_time: null,
        sign_price: null,
        sign_unit: null,
        join_num: null,
        status: null,
        deleteTime: null
      },
      // activity_status:[
      //   '未开始','未开始1','未开始2'
      // ],
      activity_status:
      {
        1: {
          value: '报名中',
        },
        2: {
          value: '待开始',
        },
        3: {
          value: '进行中',
        },
        4: {
          value: '已结束',
        },
        5: {
          value: '审核中',
        },
        6: {
          value: '审核通过',
        },
        7: {
          value: '审核不通过',
        },
      }
      ,
      // 表单参数
      form: {
        name: null,
        type: null,
        level: null,
        events_time: null,
        events_address: null,
        sign_time: null,
        sign_price: null,
        sign_unit: null,
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getUnitList() {
      listUnit().then(response => {
        this.dict = response.rows;
      }
      );
    },
    /** 查询活动列表 */
    getList() {
      this.loading = true;
      listEvents(this.queryParams).then(response => {
        this.eventsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        unitId: null,
        level: null,
        eventsTime: null,
        eventsAddress: null,
        signTime: null,
        signPrice: null,
        signUnit: null,
        joinNum: null,
        status: null,
        createTime: null,
        updateTime: null,
        deleteTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.reset();
      await this.getUnitList()
      this.open = true;
      this.title = "添加活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      updateEvents(row).then(response => {
        this.getList();
      });
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEvents(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvents(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动编号为"' + ids + '"的数据项？').then(function () {
        return delEvents(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/events/export', {
        ...this.queryParams
      }, `活动导出数据_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
